//jqGrid的配置信息
$.jgrid.defaults.width = 1000;
$.jgrid.defaults.responsive = true;
$.jgrid.defaults.styleUI = 'Bootstrap';

//工具集合Tools
window.T = {};
var baseURL = "/webspider-web";

// 获取请求参数
// 使用示例
// location.href = http://localhost:8080/index.html?id=123
// T.p('id') --> 123;
var url = function(name) {
	var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if(r!=null)return  unescape(r[2]); return null;
};
T.p = url;

//全局配置
$.ajaxSetup({
	dataType: "json",
	cache: false
});
if (Base64.extendString) {
	Base64.extendString();
}
//重写alert
window.alert = function(msg, callback){
	parent.layer.alert(msg, {time: 1000},function(index){
		parent.layer.close(index);
		if(typeof(callback) === "function"){
			callback("ok");
		}
	});
}

//重写confirm式样框
window.confirm = function(msg, callback){
	parent.layer.confirm(msg, {btn: ['确定','取消']},
	function(){//确定事件
		if(typeof(callback) === "function"){
			callback("ok");
		}
	});
}
function hasPermission(hasPermissionCode){
	var permissionList=top.permissionList;
	if (!permissionList) {//如果不存在，从后台取权限码
		$.ajax({
			  url: "../sys/user/permissionList",
			  async:false,
			  dataType: "json",
			  success: function(r){
					permissionList=r.permissionList;
					top.permissionList=permissionList;
				}
		});
	}
	for (var i = 0; i < permissionList.length; i++) {
		if(permissionList[i]==hasPermissionCode){
			return true;
			
		}
	}
	return false;
}

T.hasPermission=hasPermission;
//选择一条记录
function getSelectedRow() {
    var grid = $("#jqGrid");
    var rowKey = grid.getGridParam("selrow");
    if(!rowKey){
    	alert("请选择一条记录");
    	return ;
    }
    
    var selectedIDs = grid.getGridParam("selarrrow");
    if(selectedIDs.length > 1){
    	alert("只能选择一条记录");
    	return ;
    }
    
    return selectedIDs[0];
}
//选择一条记录
function getSelectedRowData() {
	var rowId=getSelectedRow();
	var grid = $("#jqGrid");
	var rowData = grid.getRowData(rowId);
	
	return rowData;
}
var selectWebSite = function(callback,setting){
	var defaultSetting={ids:"",maxSize:999,selectType:"checkbox",title:"选择站点"}
	if (setting) {
		defaultSetting=$.extend(true,defaultSetting,setting);
	}
	parent.layer.open({
		type:2,
		offset: '50px',
		skin: 'layui-layer-molv',
		title: defaultSetting.title,
		area: ['700px', '580px'],
		shade: 0,
		shadeClose: false,
		content: ['select/websiteselect.html?ids='+defaultSetting.ids+"&selectType="+defaultSetting.selectType, 'no'],
		btn: ['确定', '取消'],
		btn1: function (index, layero) {
			if (callback) {
				var iframeWin = window.top[layero.find('iframe')[0]['name']];//因为是用parent.layer打开的，所以要用top的范围查找
				var ids = iframeWin.getSelectedSets();
				if(ids == null){
					return ;
				}
				console.info(ids)
				callback(ids);
				
			}
			parent.layer.close(index);
        }
	});
}
T.selectWebSite = selectWebSite;

var salesSelect = function(callback,setting){
	var defaultSetting={ids:"1",maxSize:999,selectType:"checkbox",title:"选择站点"}
	if (setting) {
		defaultSetting=$.extend(true,defaultSetting,setting);
	}
	
	parent.layer.open({
		type:2,
		offset: '50px',
		skin: 'layui-layer-molv',
		title: defaultSetting.title,
		area: ['600px', '450px'],
		shade: 0,
		shadeClose: false,
		content: ['select/salesselect.html?ids='+defaultSetting.ids+"&selectType="+defaultSetting.selectType, 'no'],
		btn: ['确定', '取消'],
		btn1: function (index, layero) {
			if (callback) {
				var iframeWin = window.top[layero.find('iframe')[0]['name']];//因为是用parent.layer打开的，所以要用top的范围查找
				var ids = iframeWin.getSelectedSets();
				if(ids == null){
					return ;
				}
				console.info(ids)
				callback(ids);
				
			}
			parent.layer.close(index);
        }
	});
}
T.salesSelect = salesSelect;

var userSelect = function(callback,setting){
	var defaultSetting={
			title:"请用户",//选择器的名称
			url: '../sys/user/list',
			colModel: [			
					{ label: '用户ID', name: 'userId', index: "user_id", width: 45, key: true },
					{ label: '用户名', name: 'username', width: 75 },
					{ label: '邮箱', name: 'email', width: 90 },
					{ label: '手机号', name: 'mobile', width: 100 },
					{ label: '状态', name: 'status', width: 80, formatter: function(value, options, row){
						return value === 0 ? 
							'<span class="label label-danger">禁用</span>' : 
							'<span class="label label-success">正常</span>';
					}},
					{ label: '创建时间', name: 'createTime', index: "create_time", width: 80}
				],
			}
	if (setting) {
		defaultSetting=$.extend(true,defaultSetting,setting);
	}
	commmonSelect(callback,defaultSetting)
}
T.userSelect = userSelect;


var commmonSelect = function(callback,setting){
	var defaultSetting={
			ids:"",//默认选择的ID
			maxSize:999,//最大数
			selectType:"checkbox",//选择类型，单选还是多选
			title:"请选择",//选择器的名称
			url:'',
			postData:{},
			colModel: [			
					{ label: 'id', name: 'id', index: 'id', width: 50, key: true }
				],
			}
	if (setting) {
		defaultSetting=$.extend(true,defaultSetting,setting);
	}
	if (window.localStorage) {
		//sessionStorage.setItem("commmonSelectSetting",JSON.stringify(defaultSetting));
		top.commmonSelectSetting=defaultSetting;
	} else {
		//alert("浏览暂不支持localStorage")
	} 
	layer.open({
		type:2,
		offset: '50px',
		//skin: 'layui-layer-molv',
		title: defaultSetting.title,
		area: ['800px', '600px'],
		shade: 0,
		shadeClose: false,
		content: ['../select/commonSelect.html', 'no'],
		btn: ['确定', '取消'],
		btn1: function (index, layero) {
			if (callback) {
				var iframeWin = window[layero.find('iframe')[0]['name']];//因为是用parent.layer打开的，所以要用top的范围查找
				var ids = iframeWin.getSelectedSets();
				if(ids == null){
					return ;
				}
				var idobjects = iframeWin.getSelectedSetsObject();
				console.info(ids);
				console.info(idobjects)
				callback(ids,idobjects);
				
			}
			layer.close(index);
        }
    });
}
//选择多条记录
function getSelectedRows() {
    var grid = $("#jqGrid");
    var rowKey = grid.getGridParam("selrow");
    if(!rowKey){
    	alert("请选择一条记录");
    	return ;
    }
    
    return grid.getGridParam("selarrrow");
}
//判断是否为空
function isBlank(value) {
    return !value || !/\S/.test(value)
}
function uuid() {
	  var s = [];
	  var hexDigits = "0123456789abcdef";
	  for (var i = 0; i < 36; i++) {
	    s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
	  }
	  s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
	  s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
	  s[8] = s[13] = s[18] = s[23] = "-";
	 
	  var uuid = s.join("");
	  return uuid;
}